var PeopleMedia = PeopleMedia || {};
PeopleMedia.MiniEssayQuestions = new function () {
    this.init = function () {

        $jq('.miniessay .miniessayclosebtn').click(function () {
            $jq(".miniessay .miniessaypopupplace").hide();
        });

        $jq('.miniessay .miniessayrow1').click(function () {
            $jq(".miniessayerrorplace").hide();
            $jq(".miniessay .miniessaypopupplace").show();
        });
        PeopleMedia.MiniEssayQuestions.ConfigureMiniEssayLengths();
    };
    this.MinimumMiniEssayLengths = 100;
    this.MaximumMiniEssayLengths = 500;
    this.SetHelpEmphasisText = function(){
        var questionKey = $jq("#questionPlaceholder textarea").attr("id");

        var infoEmphasisText = 'Each answer can be between '; 

        if(questionKey.indexOf('greeting')>=0)
        {
            infoEmphasisText = 'Answer can be between ';
        }

        infoEmphasisText += PeopleMedia.MiniEssayQuestions.MinimumMiniEssayLengths + ' and ' + PeopleMedia.MiniEssayQuestions.MaximumMiniEssayLengths + ' characters'; 

        $jq('#miniessayinfoemphasis').html(infoEmphasisText);
    }

    this.ConfigureMiniEssayLengths = function () {
        PeopleMedia.MiniEssayQuestions.MinimumMiniEssayLengths = $jq('#miniessayminlenghth').val();
        PeopleMedia.MiniEssayQuestions.MaximumMiniEssayLengths = $jq('#miniessaymaxlenghth').val();
        var errorText = 'Please enter at least ' + PeopleMedia.MiniEssayQuestions.MinimumMiniEssayLengths + ' characters';

        PeopleMedia.MiniEssayQuestions.SetHelpEmphasisText();

        $jq('p.miniessayrow4p1 span').html("0");
        var textAreas = $jq('#questionPlaceholder textarea');
        textAreas.off('keyup focus');
        textAreas.on('keyup focus', function () {
            $jq(".miniessayerrorplace").hide();

            var limit = PeopleMedia.MiniEssayQuestions.MaximumMiniEssayLengths;
            var $ta = $jq(this);
            var text = $jq.trim($ta.val());
            var textWithoutDoubleSpaces = text.replace(/ +(?= )/g, '');
            var textWithoutDoubleNewlines = textWithoutDoubleSpaces.replace(/\n+(?=\n)/g, '');
            var chars = textWithoutDoubleNewlines.length;
            if (chars > limit) {
                var new_text = text.substr(0, limit);
                $ta.val(new_text);
            }
            var remain = limit - chars;
            if (remain <= 0) {
                remain = 0;
            }
            $jq('p.miniessayrow4p1 span').html(PeopleMedia.MiniEssayQuestions.MaximumMiniEssayLengths - remain);
            
        });

        $jq('p.miniessayrow4p2').html('| ' + PeopleMedia.MiniEssayQuestions.MaximumMiniEssayLengths)
        $jq('#miniessayerrormessage').html(errorText);
    };
    this.SaveEdit = function (e) {

        var questionKey = $jq("#questionPlaceholder textarea").attr("id");
        var questionValue = $jq("#questionPlaceholder textarea").val();
        var responseKeys = [];
        var responseValues = [];
        responseKeys.push(questionKey);
        responseValues.push(questionValue);
        if (questionValue.length < PeopleMedia.MiniEssayQuestions.MinimumMiniEssayLengths) {
            $jq(".miniessayerrorplace").show();
        }
        else {
            $jq.ajax({
                traditional: true,
                url: FRONT_SITE_URL+'user/profile/ProcessPageHome',
                type: 'POST',
                data: {
                    SkipCSSVerif: 'HTMLEditor',
                    QuestionKey: questionKey,
                    ResponseKeys: responseKeys,
                    ResponseValues: responseValues,
                    isQuestion: 1
                },
                success: PeopleMedia.MiniEssayQuestions.SaveEditComplete,
                error: function () {

                },
                complete: function () {

                },
                dataType: 'json'
            });
        }


    };
    this.SaveEditComplete = function (response) {
        if (response.redirect) {
            location.href = response.redirect;
            return;
        }
        
        $jq('.miniessayreviewmessage').fadeIn('fast').delay(5000).fadeOut('fast');
        if (response && response.Status == "Success") {
            $jq('#percentcompletevalue').html(response.OveralCompleteness);
            PeopleMedia.MiniEssayQuestions.UpdateMiniEssay(response.data);
        } else if (response.Message) {
            PeopleMedia.Popups.ShowGenericMessage(response.Message);
        } else {
            PeopleMedia.MiniEssayQuestions.GrabNextQuestion();
            //$jq('div.saved').text('Error Saving').fadeIn('fast').delay(2000).fadeOut('fast');
        }
        if (response && response.RequiresRefresh) {
            location.reload();
            return;
        }
        PeopleMedia.ReloadAds();
    };

    this.UpdateMiniEssay = function (data){
        PeopleMedia.MiniEssayQuestions.UpdateProfileCompletion();

        if(data == null){
            $jq('div.miniessay').hide();
            return;
        }

        $jq('#miniessayminlenghth').val(data.MinLength);
        $jq('#miniessaymaxlenghth').val(data.MaxLength);
        $jq('div.miniessayrow2 p').html(data.SiteQuestionText);
        $jq("#questionPlaceholder textarea").attr("id", data.Key);
        $jq("#questionPlaceholder textarea").val(data.ResponseText);
        PeopleMedia.MiniEssayQuestions.ConfigureMiniEssayLengths();

    };

    this.UpdateProfileCompletion = function () {
        $jq.ajax({
            traditional: true,
            url: FRONT_SITE_URL+'user/profile/ReloadProfileCompletion',
            type: 'GET',
            success:PeopleMedia.MiniEssayQuestions.GetCompletionComplete,
            error: function () {

            },
            complete: function () {

            },
            dataType: 'json'
        });
    };

    this.GetCompletionComplete = function (response) {
        if (response.redirect) {
            location.href = response.redirect;
            return;
        }

        if (response && response.Status == "Success") {
            $jq('#profilecompletion').html(response.Html);
        } else if (response.Message) {
            PeopleMedia.Popups.ShowGenericMessage(response.Message);
        } else {
            PeopleMedia.MiniEssayQuestions.GrabNextQuestion();
            //$jq('div.saved').text('Error Saving').fadeIn('fast').delay(2000).fadeOut('fast');
        }
        if (response && response.RequiresRefresh) {
            location.reload();
            return;
        }
    };
};
$jq(document).ready(PeopleMedia.MiniEssayQuestions.init);

